/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bhims;

import com.gargoylesoftware.htmlunit.WebClient;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Rein
 */
public class ScrapeFromProfileThread implements Runnable {

    public ScrapeFromProfile ParentWindow;

    @Override
    public void run() {
        try {

            UpdateLogBox("Thread Started.");
            boolean isloggedin;
            TwitterFunctions tf = new TwitterFunctions();
            CommonFunctions cf = new CommonFunctions();
            WebClient wc;

            do {
                wc = cf.initialiazeWebClient();
                String accountString = (String) ParentWindow.AccountsBlockingQueue.take();
                ParentWindow.AccountsBlockingQueue.add(accountString);
                isloggedin = tf.TwitterLogIn(wc, accountString);
                if (isloggedin) {
                    UpdateLogBox("Logged in with account: " + accountString);
                } else {
                    UpdateLogBox("Failed to log-in with account: " + accountString + " - Check Account details and Proxy.");
                }
            } while (isloggedin == false);

            while (ParentWindow.UsersBlockingQueue.isEmpty() == false) {
                String keyword = (String) ParentWindow.UsersBlockingQueue.take();
                ArrayList UsersScrapedList = new ArrayList();
                if (ParentWindow.jCheckBox1ScrapeFollowers.isSelected()) {
                    UpdateLogBox("Scraping all followers of: " + keyword + " - Please wait, this may take a few minutes");
                    UsersScrapedList.clear();
                    UsersScrapedList = tf.ScrapeUserFollowers(wc, keyword);
                    UpdateResultsTable(UsersScrapedList, keyword);
                }

                if (ParentWindow.jCheckBox2ScrapeFollowing.isSelected()) {
                    UpdateLogBox("Scraping all followings of: " + keyword + " - Please wait, this may take a few minutes");
                    UsersScrapedList.clear();
                    UsersScrapedList = tf.ScrapeUserFollowing(wc, keyword);
                    UpdateResultsTable(UsersScrapedList, keyword);
                }
                
            }


//another test

            UpdateLogBox("Finished Scraping.");

        } catch (InterruptedException ex) {
            UpdateLogBox(ex.getMessage());
        }

        if (ParentWindow.AllThreadsFinished()) {
            UpdateLogBox("~ All threads are finished ~");
        }
    }

    private void UpdateLogBox(String MessageString) {
        DateFormat df = DateFormat.getTimeInstance();
        String formattedDate = df.format(new Date());
        MessageString = formattedDate + " Thread " + Thread.currentThread().getName() + ": " + MessageString + "\n";
        ScrapeFromProfileThread.UpdateConsole uc = new ScrapeFromProfileThread.UpdateConsole();
        uc.MessageString = MessageString;
        SwingUtilities.invokeLater(uc);
    }

    private void UpdateResultsTable(ArrayList UsersScrapedList, String keyword) {
        ScrapeFromProfileThread.UpdateResults ur = new ScrapeFromProfileThread.UpdateResults();
        ur.UsersScrapedList = UsersScrapedList;
        ur.keyword = keyword;
        SwingUtilities.invokeLater(ur);
    }

    class UpdateResults implements Runnable {

        public ArrayList UsersScrapedList;
        public String keyword;

        @Override
        public void run() {
            DefaultTableModel tbm;
            tbm = (DefaultTableModel) ParentWindow.ResultsjTable1.getModel();
            for (int i = 0; i < UsersScrapedList.size(); i++) {
                Object object = UsersScrapedList.get(i);
                tbm.addRow(new Object[]{object, keyword});
            }

        }
    }

    class UpdateConsole implements Runnable {

        public String MessageString;

        @Override
        public void run() {
            ParentWindow.ConsolejTextArea2.append(MessageString);
        }
    }
}
